package com.unicom.mec.device.util;

import com.unicom.mec.common.enums.device.RunStatus;
import io.kubernetes.client.openapi.models.V1NodeCondition;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Node节点运行状态信息工具类
 *
 * @author 刘亮武
 * @date 2020年07月02日
 */
public class NodeRunInfoUtil {
    /**
     * 获取运行状态的TYPE
     */
    private static final String RUN_TYPE = "Ready";
    /**
     * 获取运行状态的TYPE
     */
    private static final String TYPE_STATUS_TRUE = "True";
    /**
     * 运行状态
     */
    public static final String RUN_STATUS = "status";
    /**
     * 运行时间
     */
    public static final String RUN_TIME = "time";

    /**
     * 获取Node运行信息
     *
     * @param conditions Node condition
     * @return
     */
    public static Map<String, Object> getRunInfo(List<V1NodeCondition> conditions) {
        Map<String, Object> result = new HashMap<>();
        // 设置默认状态为停止和运行时间为-
        result.put(RUN_STATUS, RunStatus.停止.getValue());
        result.put(RUN_TIME, "-");
        conditions.forEach(v1NodeCondition -> {
            if (RUN_TYPE.equals(v1NodeCondition.getType())) {
                if (TYPE_STATUS_TRUE.equals(v1NodeCondition.getStatus())) {
                    result.put(RUN_STATUS, RunStatus.运行.getValue());
                }
                result.put(RUN_TIME, getDatePoor(v1NodeCondition.getLastTransitionTime().toDate()));
            }
        });
        return result;
    }

    /**
     * 获取某个时间到现在间隔几天几小时几分钟
     *
     * @param date 过去某个时间
     * @return
     */
    private static String getDatePoor(Date date) {

        long nd = 1000 * 24 * 60 * 60;
        long nh = 1000 * 60 * 60;
        long nm = 1000 * 60;
        // long ns = 1000;
        // 获得两个时间的毫秒时间差异
        long diff = new Date().getTime() - date.getTime();
        // 计算差多少天
        long day = diff / nd;
        // 计算差多少小时
        long hour = diff % nd / nh;
        // 计算差多少分钟
        long min = diff % nd % nh / nm;
        return day + "天" + hour + "小时" + min + "分钟";
    }
}
